package com.sscaipiao.android.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

import com.sscaipiao.android.R;
import com.sscaipiao.android.activity.Base.BaseHallListUIActivity;
import com.sscaipiao.android.common.Constants;
import com.sscaipiao.android.model.Lottery;
import com.sscaipiao.android.model.LotteryEntry;
import com.sscaipiao.android.utils.CaipiaoServiceHelper;
import com.sscaipiao.android.utils.DateUtil;
import com.sscaipiao.android.utils.StringUtil;

public class HallActivity extends BaseHallListUIActivity implements OnClickListener, OnItemClickListener {

	private ListView mListView;
	private SimpleAdapter mAdapter ;

	private List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
	private String[] mFrom = new String[]{"hall_item_hot_image",
			"hall_item_lottery_image",
			"hall_item_txt_lottery_name",
			"hall_item_txt_lottery_date",
			"hall_item_txt_lottery_pool_num",
			"hall_item_txt_lottery_end_date",
			"hall_item_image_today"};

	private int[] mTo = new int[]{
			R.id.hall_item_hot_image,
			R.id.hall_item_lottery_image,
			R.id.hall_item_txt_lottery_name,
			R.id.hall_item_txt_lottery_date,
			R.id.hall_item_txt_lottery_pool_num,
			R.id.hall_item_txt_lottery_end_date,
			R.id.hall_item_image_today};
	@Override
	public void onCreate(Bundle savedInstanceState) {

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
				.detectDiskReads().detectDiskWrites().detectNetwork()
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
				.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
				.penaltyLog().penaltyDeath().build());

		super.onCreate(savedInstanceState);
		addContainerMain(R.layout.hall);
		setTitleText("购彩大厅");
		setupUI(false,false);
		mListView = (ListView) findViewById(R.id.hall_listView);
		//setupHallList();

		mAdapter = new SimpleAdapter(this, mList, R.layout.hall_lott_list_item, mFrom, mTo);
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);
		showProgressDialog();
		Thread thr = new Thread(mTask, "1");
		thr.start();

	}

	@Override
	protected void onResume() {
		super.onResume();

	}

	Runnable mTask = new Runnable() {
		public void run() {
			setupHallList();
			closeProgressDailog();
		}
	};
	private void setupHallList() {
		List<String> hotLottory = CaipiaoServiceHelper.getHotLottery(HallActivity.this, user);
		Log.v("hotLottory=", hotLottory.size() + "");
		List<Lottery> lotteryInfo = CaipiaoServiceHelper.getAllLotteryAward(HallActivity.this, user);
		Log.v("lotteryInfo=", lotteryInfo.size() + "");

		List<Lottery> hotLotteryInfo = new ArrayList<Lottery>();
		List<Lottery> normalLotteryInfo = new ArrayList<Lottery>();

		for (String type : hotLottory) {
			for (Lottery lottery : lotteryInfo) {
				if (lottery.getType().equals(type)) {
					hotLotteryInfo.add(lottery);
				}
			}
		}

		lotteryInfo.removeAll(hotLotteryInfo);
		normalLotteryInfo = lotteryInfo;
		lotteryInfo = hotLotteryInfo;
		lotteryInfo.addAll(normalLotteryInfo);

		Log.v("lotteryInfo2=", lotteryInfo.size() + "");

		//获取数据,这里随便加了10条数据,实际开发中可能需要从数据库或网络读取
		mList.clear();

		Map<String, Object> mMap = null;
		for (int j = 0; j < lotteryInfo.size(); j++) {
//			if (Constants.LOTTERY_TYPE_MAP.get(lotteryInfo.get(j).getType()) == null) {
//				continue;
//			}
			mMap = new HashMap<String, Object>();
			if (hotLottory.contains(lotteryInfo.get(j).getType())) {
				mMap.put("hall_item_hot_image", R.drawable.hall_item_hot);
			}

			mMap.put("hall_item_lottery_image", lotteryInfo.get(j).getIcon());
			mMap.put("hall_item_txt_lottery_name", lotteryInfo.get(j).getName());
			mMap.put("hall_item_txt_lottery_date", lotteryInfo.get(j).getPeriodNo());
			mMap.put("hall_item_txt_lottery_pool_num", StringUtil.fmtMicrometer(lotteryInfo.get(j).getCurrPool() + ""));
			mMap.put("hall_item_txt_lottery_end_date", DateUtil.getCountdownDateStr(lotteryInfo.get(j).getdLineTime()));
			if ((lotteryInfo.get(j).getdLineTime().getTime() - (new Date()).getTime()) < (1000 * 3600 * 24)) {
				mMap.put("hall_item_image_today", R.drawable.today_open);
			}
			mList.add(mMap);
		}
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				mAdapter.notifyDataSetChanged();
			}
		});

	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
//		if (R.id.hall_imageView_gengduo == v.getId()) {
//
//			Intent intent = new Intent(HallActivity.this, MoreActivity.class);
//			Bundle b = new Bundle();
//
//			//user.setToken(retUser.getToken());
//			b.putSerializable(Constants.USER, user);
//			intent.putExtras(b);
//			startActivity(intent);
//		} else if (R.id.hall_imageView_wode == v.getId()) {
//
//			Intent intent = new Intent(HallActivity.this, MineActivity.class);
//			Bundle b = new Bundle();
//
//			//user.setToken(retUser.getToken());
//			b.putSerializable(Constants.USER, user);
//			intent.putExtras(b);
//			startActivity(intent);
//		}else if(R.id.hall_imageView_kaijiang == v.getId()) {
//
//			Intent intent = new Intent(HallActivity.this, KaijiangActivity.class);
//			Bundle b = new Bundle();
//
//			//user.setToken(retUser.getToken());
//			b.putSerializable(Constants.USER, user);
//			intent.putExtras(b);
//			startActivity(intent);
//		} else if(R.id.hall_imageView_hemai == v.getId()) {
//
//			Intent intent = new Intent(HallActivity.this, TogetherHallActivity.class);
//			Bundle b = new Bundle();
//
//			//user.setToken(retUser.getToken());
//			b.putSerializable(Constants.USER, user);
//			intent.putExtras(b);
//			startActivity(intent);
//		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (((keyCode == KeyEvent.KEYCODE_BACK) ||
				(keyCode == KeyEvent.KEYCODE_HOME))) {
			showAlertDialog_Exit();
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	public void showAlertDialog_Exit() {
		AlertDialog alert = new AlertDialog.Builder(this).create();
		alert.setCanceledOnTouchOutside(true);
		alert.setMessage("是否退出？");
		alert.setButton(DialogInterface.BUTTON_POSITIVE, "是", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});
		alert.setButton(DialogInterface.BUTTON_NEGATIVE,"否",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		alert.show();
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		Class name = null;
		String lottName = (String) (mList.get(arg2)).get("hall_item_txt_lottery_name");
		if (StringUtil.isNotEmptyString(lottName)) {
			for (LotteryEntry entry : Constants.LOTT_ARRAY) {
				if (entry.getName().equals(lottName)) {
					name = entry.getActivity();
				}
			}
		}

		if (name != null) {
			if (user.getChoosen() != null) {
				user.getChoosen().clear();
			}
			Intent intent = new Intent(HallActivity.this, name);
			Bundle b = new Bundle();
			b.putSerializable("date", mList.get(arg2).get("hall_item_txt_lottery_date") + "");
			b.putSerializable("leftTime", (String) mList.get(arg2).get("hall_item_txt_lottery_end_date"));
			intent.putExtras(b);
			startActivity(intent);
		} else {
			Toast.makeText(this,"功能未实现",Toast.LENGTH_SHORT).show();
		}


	}



}
